Home:ALL Converter>MongoDB net::ERR_CONNECTION_REFUSED on server

MongoDB net::ERR_CONNECTION_REFUSED on server

Ask Time:2019-02-25T03:13:53         Author:Meghan King

Json Formatter

I am trying to make calls to my database from my react/node app hosted on a server. When run locally (using nodemon) the application is running at localhost:3000 and the server is hosted on port 4000, so I make api calls to localhost:4000. For example, localhost:4000/students displays the list of students stored in the MongoDB.

My server.js is as follows:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const PORT = 4000;
const cors = require('cors');
const mongoose = require('mongoose');
const studentRoute = require('./student.route');
const config = require('./secrets');

mongoose.Promise = global.Promise;
mongoose.connect(config.dbUri, { useNewUrlParser: true }).then(
  () => {console.log('Database is connected') },
  err => { console.log('Can not connect to the database'+ err)}
);

I am deploying my application to my site by uploading the build folder that results from npm run build to a folder called 'the-wall' (the name of the project). So to access the application I go to example.com/the-wall.

My mongoDB is hosted using MongoDB atlas, and have whitelisted my IP address and what I believe to be the IP address of the server. I am making calls using axios, with the following config:


const env = process.env.NODE_ENV; 

export const app = axios.create({
  baseURL:
    env === 'production'
      ? 'https://example.com:4000/'
      : 'http://localhost:4000/',
});

However, when I try to access example.com:4000/student I receive a net::ERR_CONNECTION_REFUSED error. As far as I can tell, mongoDB is not installed on the server. Is the URL I am using incorrect, or is there additional set up I need to do?

Note: I have also tried example.com/the-wall:4000/student, but I get a 404 error with this one.

Does the way I am trying to make a call to the database look correct? (i.e. is the URL correct with the port, etc)

Author:Meghan King,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54855535/mongodb-neterr-connection-refused-on-server
yy